﻿/*
	日历使用:
	1.第一个参数为要放入的容器ID
	2.第二个参数为回调函数名称
	3.要初始化的日期,可以为空,默认为当日
	var obj = new HotnetCalendarNameSpace.Calender("positionElementsId", "callbackFunName",date);
	obj.showCalendar();
	
	回调函数签名:
	function callbackFunName(year, month, day) {
		
	}

*/

var HotnetCalendarNameSpace = new Object();
var globalCalendarSender;
HotnetCalendarNameSpace.Calender = function(positionElementsId, callbackFunName, date) {
	if (date == null)
		this.currentDate = new Date();
	else
		this.currentDate = date;
	this.callback = callbackFunName;
	this.positionElementsId = positionElementsId;
}
HotnetCalendarNameSpace.Calender.prototype = {

	initCalendar: function() {

		var staticContent = "<div class='divCalendar'>"
											//+ "<a href='javascript:void(0)' id='calendarPreYear' style=' margin-left:40px;' ><<</a>"
											+ "<div style='text-align:center; margin:5px;'>"
												+ "<a href='javascript:void(0)' id='calendarPreMonth' ><img src='/Images/pre.gif' /></a>"
												+ "<span id='spanCalendarDateInfo'></span>"
												+ "<a href='javascript:void(0)' id='calendarNextMonth' ><img src='/Images/nxt.gif' /></a>"
											+ "</div>"
											//+ "<a href='javascript:void(0)' id='calendarNextYear' >>></a>"
											+ "<table cellspacing='0'>"
												+ "<thead>"
													+ "<tr>"
														+ "<th style='color:Red;'>日</th>"
														+ "<th>一</th>"
														+ "<th>二</th>"
														+ "<th>三</th>"
														+ "<th>四</th>"
														+ "<th>五</th>"
														+ "<th style='color:Red;'>六</th>"
													+ "</tr>"
												+ "</thead>"
												+ "<tbody id='hotnetCalendarTbody'>"
												+ "</tbody>"
											+ "</table>"
										+ "</div>";
		$("#" + this.positionElementsId).html(staticContent);
		this.today = new Date();
		//年
		this.year = this.currentDate.getFullYear();
		//月
		this.month = this.currentDate.getMonth() + 1;
		//日
		this.date = this.currentDate.getDate();
		//星期几
		this.dayOfWeek = this.currentDate.getDay();
		//一个月的第一天是星期几
		this.firstDayMonth = new Date(this.year, this.month - 1, 1);
		this.firstDayMonthOfWeek = this.firstDayMonth.getDay();
		//初始化按钮
		$("#calendarPreMonth").click(this.showPreMonth);
		$("#calendarNextMonth").click(this.showNextMonth);
		$("#calendarPreYear").click(this.showPreYear);
		$("#calendarNextYear").click(this.showNextYear);
		//初始化显示日期
		var dateStr = this.year + "年" + this.month + "月" + this.date + "日"
		$("#spanCalendarDateInfo").html(dateStr);

	},
	showCalendar: function() {
		globalCalendarSender = this;
		this.initCalendar();
		//清空body
		$("#hotnetCalendarTbody").html("");
		//渲染第一行的空行
		var firstRowBankTd = "";
		var firstRowDataTd = "";
		var firstTr = "";
		for (var i = 0; i < this.firstDayMonthOfWeek; i++)
			firstRowBankTd += "<td class='padding'></td>";

		//记录构造哪一天
		var serailDay = 0;
		//第一行剩余格渲染
		var funDateStr = "";
		for (var i = this.firstDayMonthOfWeek; i < 7; i++) {
			serailDay = i - this.firstDayMonthOfWeek + 1;
			funDateStr = "(" + this.year + "," + this.month + "," + serailDay + ")";

			//如果是今天,设置样式
			if (this.year == this.today.getFullYear() && this.month == (this.today.getMonth() + 1) && serailDay == this.today.getDate())
				firstRowDataTd += "<td class='today' onclick='" + this.callback + funDateStr + "'>" + (this.render == null ? serailDay : this.render(this.year, this.month, serailDay)) + "</td>";
			else
				firstRowDataTd += "<td onclick='" + this.callback + funDateStr + "'>" + (this.render == null ? serailDay : this.render(this.year, this.month, serailDay)) + "</td>";
		}
		firstTr = "<tr>" + firstRowBankTd + firstRowDataTd + "</tr>";
		$("#hotnetCalendarTbody").append(firstTr);

		//记录行已占用的格子数量
		var fillCount = 0;
		//除了第一行剩余天数
		var remainDay = this.getNumberOfMonth(this.year, this.month) - (7 - this.firstDayMonthOfWeek);
		var tdContent = "";
		var trContent = "";
		for (var i = 0; i < remainDay; i++) {
			serailDay++;
			fillCount++;
			funDateStr = "(" + this.year + "," + this.month + "," + serailDay + ")";

			//如果是今天,设置样式
			if (this.year == this.today.getFullYear() && this.month == (this.today.getMonth() + 1) && serailDay == this.today.getDate())
				tdContent = tdContent + "<td class='today' onclick='" + this.callback + funDateStr + "'>" + (this.render == null ? serailDay : this.render(this.year, this.month, serailDay)) + "</td>";
			else
				tdContent = tdContent + "<td onclick='" + this.callback + funDateStr + "'>" + (this.render == null ? serailDay : this.render(this.year, this.month, serailDay)) + "</td>";

			if ((i + 1) % 7 == 0) {
				trContent = "<tr>" + tdContent + "</tr>";
				$("#hotnetCalendarTbody").append(trContent);
				tdContent = "";
				trContent = "";
				fillCount = 0;
			}
		}
		if (fillCount != 0)
			for (var i = fillCount; i < 7; i++)
			tdContent += "<td class='padding'></td>";
		trContent = "<tr>" + tdContent + "</tr>";
		$("#hotnetCalendarTbody").append(trContent);

	},

	//获得月份的天数
	getNumberOfMonth: function(Year, Month) {
		var monthDay = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		if (Year % 400 == 0 || (Year % 4 == 0 && Year % 100 != 0)) {
			monthDay[1] = 29;
		}
		var monthDayNum = monthDay[Month - 1];
		return monthDayNum;
	},
	//上个月
	showPreMonth: function() {
		globalCalendarSender.currentDate.setMonth(globalCalendarSender.currentDate.getMonth() - 1);
		if (globalCalendarSender.ajaxOnMonthOrYear != null) {
			globalCalendarSender.ajaxOnMonthOrYear(globalCalendarSender.currentDate.getFullYear(), globalCalendarSender.currentDate.getMonth() + 1);
			return;
		}
		globalCalendarSender.showCalendar();
	},
	//下个月
	showNextMonth: function() {
		globalCalendarSender.currentDate.setMonth(globalCalendarSender.currentDate.getMonth() + 1);
		if (globalCalendarSender.ajaxOnMonthOrYear != null) {
			globalCalendarSender.ajaxOnMonthOrYear(globalCalendarSender.currentDate.getFullYear(), globalCalendarSender.currentDate.getMonth() + 1);
			return;
		}
		globalCalendarSender.showCalendar();
	},
	//上个月
	showPreYear: function() {
		globalCalendarSender.currentDate.setFullYear(globalCalendarSender.currentDate.getFullYear() - 1);
		if (globalCalendarSender.ajaxOnMonthOrYear != null) {
			globalCalendarSender.ajaxOnMonthOrYear(globalCalendarSender.currentDate.getFullYear(), globalCalendarSender.currentDate.getMonth() + 1);
			return;
		}
		globalCalendarSender.showCalendar();
	},
	//下个月
	showNextYear: function() {
		globalCalendarSender.currentDate.setFullYear(globalCalendarSender.currentDate.getFullYear() + 1);
		if (globalCalendarSender.ajaxOnMonthOrYear != null) {
			globalCalendarSender.ajaxOnMonthOrYear(globalCalendarSender.currentDate.getFullYear(), globalCalendarSender.currentDate.getMonth() + 1);
			return;
		}
		globalCalendarSender.showCalendar();
	},
	setRender: function(render) {
		this.render = render;
	},
	setAjaxOnMonthOrYear: function(callback) {
		this.ajaxOnMonthOrYear = callback;
	}
};